/**
  ******************************************************************************
  * @file    uart.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *           
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"


uint8   ReceiveArray[52];
uint8 	BTTestMode;
//uint8   outbyte[8]; 
uint8   inbyte[16];
uint8 	BPLevelType;
uint8 	UartType;
uint8 	KeyNumber;
uint8 	VoiceType;
uint8	MemorySetType;
uint8	DeflateValueType;
uint8 	InflateRate;
uint8 	ConnectFlag;
uint8   SendBuffer[5];


uint16  InflateValue;	
uint8   InflateValueSet;
uint8   InflateValueCount;
uint16  InflateValueMax;
uint16   DeflateValueSet;

uint16 	DIAGainValue;
uint16 	SYSGainValue;	

uint8   DeflateValue; 

uint8   PulseRateDIM[33];

uint16  MaxAmpValue;         
uint16  DetermineValue; 

uint8   MaxInterval;
uint8   MinInterval;     					
uint16  AmpValue[5]; 

uint16  DiastolicValue;  

uint8	MaxYear;
uint8 	YEARINIT;


uint8   TruePulseCount; 
uint8   ResultCode; 

uint8 ModifyFlag=0;
uint8 AllTimeFlag=0;
uint16 AUTOOFFTIME;

uint16  PulseRamSum;				
uint16  PulseRamSumNew;
uint16  PulseRamTemp;
volatile union word_data		SystolicValue; 	
volatile union	word_data	PulseRam[63]; 
//*************************预适应
uint8 YSYFlag=0;
uint8 FSTime;
uint8 YPTime;
uint8 YSYCount;
uint16 YSYPress;




void User_Uart0_Init(void)
{
		RCC_APB2PeriphClockConfig(RCC_APBPeriph_UART0,RCC_UARTCLK_SYSCLK,0);
	   RCC_APBPeriphClockCmd(RCC_APBPeriph_UART0, ENABLE);                    //外设时钟使能
	   
	   UART_InitTypeDef  UART_InitStruct;
	   UART_InitStruct.UART_BaudRate=9600;
	   UART_InitStruct.UART_Mode=UART_Mode_TRx;
	   UART_InitStruct.UART_Parity=UART_Parity_None;
	   UART_InitStruct.UART_StopBits=UART_StopBits_1;
	   UART_InitStruct.UART_WordLength=UART_WordLength_8b;
	   UART_InitStruct.UART_CarryWave=UART_CarryWave_None; 
	   
	   UART_Init(UART0,&UART_InitStruct);
	   
	   UART_ClearFlagStatus(UART0, UART_Flag_RXNE); 
	   UART_ITConfig(UART0, UART_IT_RXNEIE, ENABLE);
	   UART_ITConfig(UART0, UART_IT_TXEIE, DISABLE);
	   
	   UART_Cmd(UART0,ENABLE);
   
 //  BT_POWER_On;

}




//=======================================================================


void Uart0_Rev_Interrupt(void)	
{
	uint16 temp;
	uint8 i=0;
	uint16 ReceiveCount=0;
	i = 0;
	if(UART_GetFlagStatus(UART0,UART_Flag_RXNE))//rev 
	 {   
	   do{ 
			 if(UART_GetFlagStatus(UART0,UART_Flag_RXNE))//rev 
			 {
				  ReceiveArray[i]=UART_ReceiveData(UART0);
				   UART_ClearFlagStatus(UART0, UART_Flag_RXNE);
				  i++;
				  ReceiveCount=0;
			 }  
			 ReceiveCount++;
		}while((i<16)&&(ReceiveCount<6000));
		
	 }
	if(UART_GetFlagStatus(UART0,UART_Flag_TC))
	{
			// Clear transmit flag
			goto UartOver;
    }
		
	if(IsCalibrate)
	{
		if(ReceiveArray[0]==0xA5&&ReceiveArray[1]==0xA6)
		{
			BTTestMode=1;
			delay(4);
			Rs232_SendByte(0xa5);
			Rs232_SendByte(0x59);
			delay(4);
			Rs232_SendByte(0xa5);
			Rs232_SendByte(0x59);
			delay(4);
			Rs232_SendByte(0xa5);
			Rs232_SendByte(0x59);
			delay(4);
			Rs232_SendByte(0xa5);
			Rs232_SendByte(0x59);
			goto UartOver;
		}
		if(BTTestMode==1)
		{
			if(ReceiveArray[0]==0x01)
			{
				BTTestMode=2;
			}
			else if(ReceiveArray[0]==0x02)
			{
				BTTestMode=3;
			}
		}
		
		if(ReceiveArray[0]==0xAA&&ReceiveArray[1]==0x55&&ReceiveArray[10]==0x55&&ReceiveArray[11]==0xAA)
		{
			outbyte[0]=0;
			for(i=0;i<7;i++)
			{
				inbyte[i]=ReceiveArray[i+2];
				outbyte[0]+=inbyte[i];
			}
			inbyte[7]=ReceiveArray[9];
			if(inbyte[7]==outbyte[0])
			{
				functionOKFlag		=1;
				BPLevelType			=inbyte[0]>>4;
				UartType			=inbyte[0]&0x0f;
				KeyNumber			=inbyte[1]>>4;
				VoiceType			=inbyte[1]&0x0f;
				MemorySetType		=inbyte[2]>>4;
				DeflateValueType	=inbyte[2]&0x0f;
				InflateRate			=inbyte[3];
				SystemSetFlag.b 	=inbyte[4];
				SystemSetFlag2.b	=inbyte[5];
				SystemSetFlag3.b	=inbyte[6];	
			}			
		}
		if(ReceiveArray[0]==0xff&&ReceiveArray[1]==0x00)
		{
			SendRomFlag=1;
		}
	}		
		
	if(UartType>0)	//蓝牙模式或蓝牙USB模式
	{
		if((ReceiveArray[0]==0xFF)&&(ReceiveArray[1]==0xFF))	//某些客户指令加了0XFF 0XFF
		{
			for(i=0;i<14;i++)
			{
				ReceiveArray[i]=ReceiveArray[i+2];
			}
		}
		if((ReceiveArray[0]==0xFD)&&(ReceiveArray[1]==0xFD)&&(ReceiveArray[2]==0xFA)&&(ReceiveArray[3]==0x05))				//FD FD FA 05  开始量测
			{
				if(ReceiveArray[4]>16&&ReceiveArray[4]<100&&ReceiveArray[5]<13&&ReceiveArray[6]<32&&ReceiveArray[7]<25)  	//Year Month Day Hour Minute Second
					{
						Year.b	=ReceiveArray[4];														//update date and time
						Month	=ReceiveArray[5];
						Day		=ReceiveArray[6];
						Hour	=ReceiveArray[7];
						Minute	=ReceiveArray[8];
						Second	=ReceiveArray[9];
					}
				BTFlag=1;
				ConnectFlag=1;
				IsPowerOn=1;
				SendBuffer[0]=0xFD;
				SendBuffer[1]=0xFD;
				SendBuffer[2]=0x06;
				SendBuffer[3]=0x0D;
				SendBuffer[4]=0x0A;
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);									////回应2次： FD FD 06 0D 0A
					}	
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}	
				goto UartOver;		
			}
			
		if((ReceiveArray[0]==0xFD)&&(ReceiveArray[1]==0xFD)&&(ReceiveArray[2]==0xFA)&&(ReceiveArray[3]==0x07))				//FD FD FA 06  传记忆
			{
				if(ReceiveArray[4]>16&&ReceiveArray[4]<100&&ReceiveArray[5]<13&&ReceiveArray[6]<32&&ReceiveArray[7]<25)  	//Year Month Day Hour Minute Second
					{
						Year.b	=ReceiveArray[4];														//update date and time
						Month	=ReceiveArray[5];
						Day		=ReceiveArray[6];
						Hour	=ReceiveArray[7];
						Minute	=ReceiveArray[8];
						Second	=ReceiveArray[9];
					}		
//				delay(4);
				SendMemoryFlag=1;
				Rs232_SendByte(0xfd);	
				Rs232_SendByte(0xfd);	
				Rs232_SendByte(0xfa);	
				Rs232_SendByte(0x07);	
				Rs232_SendByte(0x0d);	
				Rs232_SendByte(0x0a);	
			}
							
		if((ReceiveArray[0]==0xFD)&&(ReceiveArray[1]==0xFD)&&(ReceiveArray[2]==0xFA)&&(ReceiveArray[3]==0x60))				//传完成记忆，并确认
			{
				SendOKFlag=1;
				SendBuffer[0]=0xFD;
				SendBuffer[1]=0xFD;
				SendBuffer[2]=0x60;
				SendBuffer[3]=0x0D;
				SendBuffer[4]=0x0A;
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}	
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}				
			}
			if((ReceiveArray[0]==0xFD)&&(ReceiveArray[1]==0xFD)&&(ReceiveArray[2]==0xFA||ReceiveArray[2]==0xFe)&&(ReceiveArray[3]==0x06))				//关机指令
			{
				IsPowerOn=0;
				SendBuffer[0]=0xFD;
				SendBuffer[1]=0xFD;
				SendBuffer[2]=0x07;
				SendBuffer[3]=0x0D;
				SendBuffer[4]=0x0A;
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}	
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}				
			}
			if(ReceiveArray[0]==0xfd&&ReceiveArray[1]==0xfd&&ReceiveArray[2]==0xfa&&ReceiveArray[3]==0x0a)//预适应模式
			{
				YSYFlag=1;
				FSTime=ReceiveArray[4];
				YPTime=ReceiveArray[5];
				YSYCount=ReceiveArray[6];
				YSYPress=ReceiveArray[7];
				YSYPress*=256;
				YSYPress+=ReceiveArray[8];
			}
			
			if(ReceiveArray[0]==0xfd&&ReceiveArray[1]==0xfd&&ReceiveArray[2]==0xfa&&ReceiveArray[3]==0x08)//更改关机时间
			{
				ModifyFlag=1;
				if(ReceiveArray[4]==0) AllTimeFlag=1;
				else 
				{
					AllTimeFlag=0;
					if(ReceiveArray[4]>20) ReceiveArray[4]=20;
					temp=ReceiveArray[4]*60;
					temp*=50;
					AUTOOFFTIME=temp;
				}
				AutoOffTimew=0;
				SendBuffer[0]=0xFD;
				SendBuffer[1]=0xFD;
				SendBuffer[2]=0x08;
				SendBuffer[3]=0x0D;
				SendBuffer[4]=0x0A;
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}	
//				delay(4);
				for(i=0;i<5;i++)
					{
						Rs232_SendByte(SendBuffer[i]);
					}	
			}
			if(ReceiveArray[0]==0xfd&&ReceiveArray[1]==0xfd&&ReceiveArray[2]==0xfa&&ReceiveArray[3]==0x09)//更新时间
			{
				if(ReceiveArray[4]>=YEARINIT&&ReceiveArray[4]<=99&&ReceiveArray[5]>=1&&ReceiveArray[5]<=12&&ReceiveArray[6]>=1
					&&ReceiveArray[6]<=31&&ReceiveArray[7]<=23&&ReceiveArray[8]<=59&&ReceiveArray[9]<=59)
				{
					Year.b=ReceiveArray[4];
					Month=ReceiveArray[5];
					Day=ReceiveArray[6];
					Hour=ReceiveArray[7];
					Minute=ReceiveArray[8];
					Second=ReceiveArray[9];
					
					SendBuffer[0]=0xFD;
					SendBuffer[1]=0xFD;
					SendBuffer[2]=0x09;
					SendBuffer[3]=0x0D;
					SendBuffer[4]=0x0A;
//					delay(4);
					for(i=0;i<5;i++)
						{
							Rs232_SendByte(SendBuffer[i]);
						}	
//					delay(4);
					for(i=0;i<5;i++)
						{
							Rs232_SendByte(SendBuffer[i]);
						}	
				}
			}
		}
	
	if(UartType==3)		//GPRS模式
	{
		
	}
	
UartOver:
nop4();
//	RXIEnable;
	RXEnable;
}

/*
void UART_Tx_Byte(uint8 Tx_data)
{
		S0BUF	=	Tx_data;
		while(!TI0);
		TI0=0;
}*/

void Rs232_SendByte (uint8 SendData)
{
		uint16 sendcount=0;	
		UART_SendData(UART0,SendData);
		do{
			sendcount++;
		}while(!UART_GetFlagStatus(UART0,UART_Flag_TC)&&(sendcount<2000));//发送完成TC=1
			
}

//******************************************************************************
//Send_filter
//******************************************************************************
void SendPressure(void)
{
	if (T125ms == 0) {OneShotRX = 0;}
	else
	{
			if (OneShotRX == 0)
		  {
				if(UartType==6)
				{
					Rs232_SendByte(0x20);
					if(PressuremmHgw<=255) Rs232_SendByte(PressuremmHgw);
					else Rs232_SendByte(255);
				}
				else
				{
					Rs232_SendByte(0xFD);
					Rs232_SendByte(0xFD);
					Rs232_SendByte(0xFB);
					Rs232_SendByte(PressuremmHg2b);
					Rs232_SendByte(PressuremmHg1b);
					Rs232_SendByte(0x0D);
					Rs232_SendByte(0x0A);
				}
				OneShotRX=1;
		   }
	} 

}	
	
void SendTest(void)
{
	Rs232_SendByte(0xFD);
	Rs232_SendByte(0xFD);	
	Rs232_SendByte(0xFC);
	Rs232_SendByte(100);	
	Rs232_SendByte(80);	
	Rs232_SendByte(75);
	Rs232_SendByte(0x0D);
	Rs232_SendByte(0x0A);	
}
	
void SendResult(void)
	{
		if(UartType==6)
		{
			Rs232_SendByte(0x1C);
			Rs232_SendByte(0x00);
			Rs232_SendByte(SystolicValuew);
			Rs232_SendByte(0x00);
			Rs232_SendByte(DiastolicValue);	
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(PulseRate);
			Rs232_SendByte(0x00);
			Rs232_SendByte(PADFlag);
			Rs232_SendByte(0x00);
		}
		else
		{
			Rs232_SendByte(0xFD);
			Rs232_SendByte(0xFD);	
			Rs232_SendByte(0xFC);
			Rs232_SendByte(SystolicValuew);	
			Rs232_SendByte(DiastolicValue);	
			Rs232_SendByte(PulseRate);
			Rs232_SendByte(0x0D);
			Rs232_SendByte(0x0A);
		}
	}		

void Send_filter_test(void)
{
	Rs232_SendByte(254);
	Rs232_SendByte(PressuremmHg1b);
//	Rs232_SendByte(DIAGainValue);
	Rs232_SendByte(OscilloSignal01b);
//	Rs232_SendByte(AmpValue[0]);
	if(IsPump)
		{//1实时  2前一笔 3更前一笔
			Rs232_SendByte(AmpValue[1]);
			Rs232_SendByte(FuzzyMaxValue);
//			Rs232_SendByte(DeflateValue);
//			Rs232_SendByte(VALVEPWMValue);
		}
	else
		{
			Rs232_SendByte(AmpValue[2]);
			Rs232_SendByte(MaxAmpValue);
	//		Rs232_SendByte(DeflateValue);
	//		Rs232_SendByte(VALVEPWMValue);
		}
}

void Send_filter_test1(void)
{
	Rs232_SendByte(254);
	Rs232_SendByte(PressuremmHg1b);
	Rs232_SendByte(OscilloSignal01b);
	Rs232_SendByte(InflateValue);
	Rs232_SendByte(InflateValueSet);
}	
void SendResultTest(void)
{
	Rs232_SendByte(253);
	Rs232_SendByte(253);	
	Rs232_SendByte(SystolicValuew);	
	Rs232_SendByte(DiastolicValue);	
	Rs232_SendByte(PulseRate);
	Rs232_SendByte(UparmScaleValue);	
	Rs232_SendByte(SYSGainValue);
	Rs232_SendByte(DIAGainValue);		
}	
////////////////////////////////////////////		
void Send_PulseInterval(void)
	{
	unsigned char i;
	Rs232_SendByte(252);
	Rs232_SendByte(252);
	for(i=0;i<=HRPulseCount;i++)
	{Rs232_SendByte(PulseRateDIM[i]);}	
	Rs232_SendByte(250);
	Rs232_SendByte(250);
	}
	
void Send_PulseRam(void)
{
	unsigned char i;
	Rs232_SendByte(252);
	Rs232_SendByte(252);
	Rs232_SendByte(SystolicValuew);	
	Rs232_SendByte(DiastolicValue);	
	Rs232_SendByte(PulseRate);
	Rs232_SendByte(UparmScaleValue);
	Rs232_SendByte(SYSGainValue);
	Rs232_SendByte(DIAGainValue);
	Rs232_SendByte(0);		
	for(i=0;i<=TruePulseCount;i++)
		{Rs232_SendByte(PulseRam[i].b[0]);}		
	Rs232_SendByte(0);
	for(i=0;i<=TruePulseCount;i++)
		{Rs232_SendByte(PulseRam[i].b[1]);}	
	Rs232_SendByte(0);		
	for(i=0;i<=HRPulseCount;i++)
		{Rs232_SendByte(PulseRateDIM[i]);}			 
}
void Send_PulseRam1(void)
{
	unsigned char i;
	Rs232_SendByte(252);
	Rs232_SendByte(252);
	for(i=0;i<=TruePulseCount;i++)
		{
			Rs232_SendByte(PulseRam[i].b[0]);
			Rs232_SendByte(PulseRam[i].b[1]);
		}
	Rs232_SendByte(SYSGainValue);
	Rs232_SendByte(DIAGainValue);
	Rs232_SendByte(250);
	Rs232_SendByte(250);		
}
void Send_LowBattery(void)	
{
	if(UartType==6)
	{
			Rs232_SendByte(0x1C);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);	
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(0x0B);
	}
	else
	{
		Rs232_SendByte(0xFD);
		Rs232_SendByte(0xFD);	
		Rs232_SendByte(0xFD);
		Rs232_SendByte(0x0B);	
		Rs232_SendByte(0x0D);
		Rs232_SendByte(0x0A);
	}
}
void Send_ErrorCode(void)
{
	if(UartType==6)
	{
			Rs232_SendByte(0x1C);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);	
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(0x00);
			Rs232_SendByte(0);
			Rs232_SendByte(ResultCode);
	}
	else
	{
		Rs232_SendByte(0xFD);
		Rs232_SendByte(0xFD);	
		Rs232_SendByte(0xFD);
		Rs232_SendByte(ResultCode);	
		Rs232_SendByte(0x0D);
		Rs232_SendByte(0x0A);
	}
}

void Sendall(void)
{
	unsigned char Sendmonth;
	unsigned char user;
	if(UserA) user=1;
	else user=2;
	Sendmonth=(inbyte[3]&0xc0)/64+(inbyte[4]&0xc0)/16;
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xfe);
	Rs232_SendByte(user);
	Rs232_SendByte(inbyte[0]);
	Rs232_SendByte(inbyte[1]);
	Rs232_SendByte(inbyte[2]);
	Rs232_SendByte(20);
	Rs232_SendByte(inbyte[6]);
	Rs232_SendByte(Sendmonth);
	Rs232_SendByte(inbyte[3]&0x3f);
	Rs232_SendByte(inbyte[4]&0x3f);
	Rs232_SendByte(inbyte[5]&0x7f);
	Rs232_SendByte(0x0d);
	Rs232_SendByte(0x0a);
}

void Sendlast(void)
{
	unsigned char Sendmonth;
	unsigned char user;
	if(UserA) user=1;
	else user=2;
	Sendmonth=(inbyte[3]&0xc0)/64+(inbyte[4]&0xc0)/16;
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xff);
	Rs232_SendByte(user);
	Rs232_SendByte(inbyte[0]);
	Rs232_SendByte(inbyte[1]);
	Rs232_SendByte(inbyte[2]);
	Rs232_SendByte(20);
	Rs232_SendByte(inbyte[6]);
	Rs232_SendByte(Sendmonth);
	Rs232_SendByte(inbyte[3]&0x3f);
	Rs232_SendByte(inbyte[4]&0x3f);
	Rs232_SendByte(inbyte[5]&0x7f);
	Rs232_SendByte(0x0d);
	Rs232_SendByte(0x0a);
}


void SendEmpty(void)
{
	unsigned char user;
	if(UserA) user=1;
	else user=2;
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xfd);
	Rs232_SendByte(0xff);
	Rs232_SendByte(user);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0);
	Rs232_SendByte(0x0d);
	Rs232_SendByte(0x0a);
}



void SendAllMemoryData(void)
{
#if 1
//	unsigned char 	i=1;
//StartToSendAllmemory:
	RXIEnable;
	DarkAllIcon();
	UserABDisplay();
	delay(10);
	MemoryMode=1;	
	checkmemorybankAB();
	MemoryMode=1;				
	EEPROMin = 0;
	readM24L16();	
//	MemoryEndIndex = inbyte[0]-1;					//reload first address	//用于找最后一笔记忆值的index	
	MemoryEndIndex -= 1;
	if(MemoryFull)	{MemoryIndex = MemorySet;}		//load memory count //用于循环计数 如果full,则全部.
	else	{MemoryIndex = MemoryEndIndex;}	//如果没有full,则最后一笔就是全部笔数.	
	
	if((MemoryEndIndex==0 && MemoryFull==0)||(ReadEepromError==1)||(MemoryEndIndex>MemorySet)) 	{SendEmpty();	goto SendAllMemoryOver;}					
	while(MemoryIndex>0)
		{
			EEPROMin=MemoryEndIndex;
			readM24L16();
			DisplayToMemorySet(MemoryIndex);
			if(MemoryIndex>1) Sendall();
			else Sendlast();
			delay(10);
//nosend:
			MemoryIndex--;
			MemoryEndIndex--;
			if(MemoryEndIndex==0) MemoryEndIndex=MemorySet;
		}
	SendAllMemoryOver:
	SendOKFlag=0;
	DarkAllIcon();
	MemoryMode=0;
	DisplayToMemorySet(0);
	AutoOffTimew=0;
	while(KEY_MEMORYUP&&AutoOffTimew<100&&!SendOKFlag);
	if(!SendOKFlag)
		{
			DisplayHalfByte(0xe,SYSHundred);
			DisplayHalfByte(0xb,SYSTen);
			DisplayHalfByte(0xb,SYSSingle);
			BUZZER_EN;
			delay(43);
			BUZZER_DIS;  
			AutoOffTimew=0;
			while(!KEY_MEMORYUP&&AutoOffTimew<85);
			IsPowerOn=0;
			MemoryMode=0;
		}  
	if(SendOKFlag)
	{
			DarkAllIcon();
			CLRMemoryBank();
			flashCL();																			//CL memory
			IsPowerOn=0;
			MemoryMode=0;
	}
#endif	  
}


void sendfunction(void)	//发送功能
{
	uint8 i;
	outbyte[0]=BPLevelType<<4;
	outbyte[0]|=UartType;
 	outbyte[1]=KeyNumber<<4;  
	outbyte[1]|=VoiceType;
	outbyte[2]=MemorySetType<<4;
	outbyte[2]|=VoiceType2;
	outbyte[3]=SystemSetFlag4.b;                       
	outbyte[4]=SystemSetFlag.b; 
	outbyte[5]=SystemSetFlag2.b;              
	outbyte[6]=SystemSetFlag3.b;  
	outbyte[7]=0;
	for(i=0;i<7;i++) outbyte[7]+=outbyte[i];
	Rs232_SendByte(0x55);
	Rs232_SendByte(0xAA);
	for(i=0;i<8;i++) Rs232_SendByte(outbyte[i]);
	Rs232_SendByte(0xAA);
	Rs232_SendByte(0x55);
}


//暂时未使用
void sendrom(void)
{
	uint8 i;
	for(i=0;i<8;i++)
	{
		//inbyte[i]=Byte_RDNO(0xff40+i);//待确认内容和地址
		//outbyte[i]=Byte_RDNO(0xff00+i);
	}
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x40);
	for(i=0;i<8;i++) Rs232_SendByte(inbyte[i]);
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x40);	

	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x00);
	for(i=0;i<8;i++) Rs232_SendByte(outbyte[i]);
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x00);		
	
	for(i=0;i<8;i++)
	{
		//inbyte[i]=Byte_RDNO(0xffc0+i);
		//outbyte[i]=Byte_RDNO(0xff80+i);
	}
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0xc0);
	for(i=0;i<8;i++) Rs232_SendByte(inbyte[i]);
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0xc0);	

	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x80);
	for(i=0;i<8;i++) Rs232_SendByte(outbyte[i]);
	Rs232_SendByte(0xFF);
	Rs232_SendByte(0x80);	
}

